package com.ossean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.dao.DBDest;
import com.ossean.dao.DBSource;
import com.ossean.dao.TransferPrjDao;
import com.ossean.model.EddRelations;
import com.ossean.util.TransferProjectsUtil;
import com.ossean.util.TransferProjectsUtil2;


@Component
public class TransferProjects {
	Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private DBSource dbSource;
	@Resource
	private DBDest dbDest;
	@Resource
	private TransferPrjDao transferPrjDao;
	@Qualifier("transferProjectsUtil2")
	@Autowired
	private TransferProjectsUtil2 transferProjectsUtil2;
	
	
	private String pointerTableName =  TableName.pointerTableName;
	private String sourceTableName = TableName.eddRelationTableName;
	private String targetTableName = TableName.openSourceProjectsTableName;
	private String taggingTableName = TableName.taggingsTableName;
	
	private int batchSize = 100;
	//private static Set<Integer> haveTransferSet;
	
	//读指针
    public int readPointer(String table, String source, String target){
		int pointer = 1;
		//pointer的初始值应该从数据库中读出最小的id
		try {
			pointer = dbSource.getMinId(source);
		} catch (Exception e){
			//表示表中没有数据
			logger.info("No item in this table");
			dbSource.insertPointer(table, source, target, 1);
			return 1;
		}
		
		try {
			pointer = dbSource.getPointer(table, source, target);
		} catch(Exception e) {
			logger.info("No such pointer! Create one");
			dbSource.insertPointer(table, source, target, pointer);
		}
		return pointer;
	}
	
	public void start(){
		
		//haveTransferSet = new HashSet<Integer>();
		//haveTransferSet = dbDest.selectOpenSourceProjectsIds(targetTableName);
		int delCount = 0;
		logger.info("begin transfer projects");
		while(true){
			int startId = readPointer(pointerTableName, sourceTableName, targetTableName);
			List<EddRelations> eddRelationList = transferPrjDao.getEddRelationList(sourceTableName, startId, batchSize);
			if(eddRelationList.size() == 0){
				try {
					System.out.println("freecode : "+delCount);
					logger.info("没有数据了，休眠10min");
					Thread.sleep(3*1000L);
					continue;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			for(EddRelations relation:eddRelationList){
				boolean isUpdate = false;
				String gather_projects_ids = relation.getGather_projects_ids();
				try {
					gather_projects_ids = gather_projects_ids.substring(1, gather_projects_ids.length() - 1);
				} catch (Exception e1) {
					System.out.println(relation.getGather_projects_ids());
					e1.printStackTrace();
				}
				String[] idsArray = gather_projects_ids.split(",");
				for(int i = 0; i < idsArray.length; i++){
					int id = Integer.parseInt(idsArray[i]);
					if(null != dbDest.selectOpenSourceProjectsItem(targetTableName,id)){
						isUpdate = true;
						break;
					}
				}
				int prjId = transferProjectsUtil2.handleOneRelation(relation,isUpdate);
				if(prjId==-1)//统计freecode
					delCount++;
				dbSource.updatePointer(pointerTableName, sourceTableName, targetTableName, relation.getId()+1);
			}
			
		}
	}
	
	public static void main(String[] args){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		TransferProjects Main = applicationContext.getBean(TransferProjects.class);
		Main.start();
	}
	
	public static String getTargetTable(int ospId){
		String targetTableName = "";
		if(ospId >= 770000){
			targetTableName = "relative_memo_to_open_source_projects_70";
		}
		else{
			int a = 1 + ospId/11000;
			targetTableName = "relative_memo_to_open_source_projects_" + a;
		}

		return targetTableName;
	}

}
